﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ReporterEngine;

namespace ReporterSystem
{
    public class MyDocumentsProvider : FilesProvider
    {
        public override System.IO.Stream GetImage(string projectName, string imageName)
        {
            string filepath = ReportApp.MyDocumentsFolder;
            filepath = Path.Combine(filepath, projectName, "export","Images", imageName);

            return File.OpenRead(filepath);
        }

        public override System.IO.Stream GetTemplate(string projectName, string templateName)
        {
            string filepath = ReportApp.MyDocumentsFolder;
            var tplpath = Path.Combine(filepath, projectName, "templates", templateName + ".docx");

            var tplfilepath = Path.Combine(filepath, projectName, "templates", "__"+templateName + ".docx");

            File.Copy(tplpath, tplfilepath, true);

            return File.OpenRead(tplfilepath);
        }

        public override System.IO.Stream GetCif(string projectName)
        {
            string filepath = ReportApp.MyDocumentsFolder;
            filepath = Path.Combine(filepath, projectName,"export", "cif.xml");

            return File.OpenRead(filepath);
        }

        public override Stream GetResult(string projectName, string resultName, ReportSettingsArgs settings)
        {
            //create if not exists
            //need source to prepare result file
            string resultpath = ReportApp.MyDocumentsFolder;
            resultpath = Path.Combine(resultpath, projectName, "results", resultName + ".docx");

            return File.Open(resultpath,FileMode.Open,FileAccess.ReadWrite);
        }

        public override bool ImageExists(string projectName, string imageName)
        {
            string filepath = ReportApp.MyDocumentsFolder;
            filepath = Path.Combine(filepath, projectName, "export", "Images", imageName);

            return File.Exists(filepath);
        }

        public override void CopyTemplateAsResult(string projectName, string templateName, string resultName)
        {
            string filepath = ReportApp.MyDocumentsFolder;
            filepath = Path.Combine(filepath, projectName, "templates", templateName + ".docx");

            string resultpath = ReportApp.MyDocumentsFolder;
            resultpath = Path.Combine(resultpath, projectName, "results", resultName + ".docx");

            File.Copy(filepath, resultpath, true);
        }

        public override byte[] getResultData(string projectName, string templateName)
        {
            throw new NotImplementedException();
        }
    }
}
